package com.sl.mes.app.service;

import com.qiniu.http.Response;
import com.sl.mes.app.controller.MesAuthController;
import com.sl.mes.app.dao.agg.AggMaterial;
import com.sl.mes.app.dao.agg.AggPsnDoc;
import com.sl.mes.app.dao.agg.AggPsnJob;
import com.sl.mes.db.itf.org.*;
import com.sl.mes.db.service.MesUserService;
import com.sl.mes.db.service.bd.BdMaterialService;
import com.sl.mes.db.service.bd.BdMeasdocService;
import com.sl.mes.db.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.sl.mes.app.dao.UserInfo;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserInfoService {
	private static final Logger logger = LoggerFactory.getLogger(MesAuthController.class);
	@Autowired
	private MesUserService userService;

	@Autowired
	private ISmUser iSmUserService;

	@Autowired
	private IOrgGroup orgGroupService;

	@Autowired
	private IOrgOrgs orgOrgsService;

	@Autowired
	private IOrgDept orgDeptService;

	@Autowired
	private IBdPsnDoc iBdPsnDocService;

	@Autowired
	private BdMaterialService bdMaterialService;

	@Autowired
	private BdMeasdocService bdMeasdocService;

	public UserInfo getInfo(Integer userId) {
		MesUser user = userService.findById(userId);
		Assert.state(user != null, "用户不存在");
		UserInfo userInfo = new UserInfo();
		userInfo.setNickName(user.getNickname());
		userInfo.setAvatarUrl(user.getAvatar());
		return userInfo;
	}

	/**
	 * 获取NC用户信息
	 * @param userId
	 * @return
	 */
	public SmUser getNCUserInfo(String userId){
		//关联NC用户信息
		SmUser smUser= iSmUserService.findById(userId);
		smUser.setUserPassword("");
		return smUser;
	}

	public AggPsnDoc getPsnDoc(String pk_psndoc){
		//关联NC人员信息
		//人员信息
		AggPsnDoc psnDoc=new AggPsnDoc();

		//人员基本信息(人员档案中获取)
		BdPsndoc bdPsndoc= iBdPsnDocService.findById(pk_psndoc);
		psnDoc.setBdPsndoc(bdPsndoc);

		//人员任职信息(从任职记录中获取)
		List<AggPsnJob> lstPsnJob=new ArrayList<AggPsnJob>();
		List<BdPsnjob> lstBdPsnJob = iBdPsnDocService.getPsnJobByPsnId(bdPsndoc.getPkPsndoc());
		for (BdPsnjob item : lstBdPsnJob) {
			AggPsnJob psnJob=new AggPsnJob();
			//任职记录明细
			psnJob.setBdPsnjob(item);
			//任职记录的部门
			OrgDept orgDept=orgDeptService.findById(item.getPkDept());
			psnJob.setOrgDept(orgDept);
			//任职记录的组织
			OrgOrgs orgOrgs=orgOrgsService.findById(item.getPkOrg());
			psnJob.setOrgOrgs(orgOrgs);
			//任职记录的集团
			OrgGroup orgGroup=orgGroupService.findById(item.getPkGroup());
			psnJob.setOrgGroup(orgGroup);

			lstPsnJob.add(psnJob);
			psnDoc.setLstPsnJob(lstPsnJob);
		}
		return psnDoc;
	}

	public List<AggMaterial> queryAllLimit(Integer pageNo, Integer pageSize){
		List<AggMaterial> materialLst=new ArrayList<>();
		List<BdMaterial> bdMaterialsLst = bdMaterialService.queryAll(pageNo, pageSize);
		for (BdMaterial item: bdMaterialsLst ){
			AggMaterial material=new AggMaterial();
			material.setBdMaterial(item);


			//物料的组织
			OrgOrgs orgOrgs=orgOrgsService.findById(item.getPkOrg());
			material.setOrgOrgs(orgOrgs);
			//物料的集团
			OrgGroup orgGroup=orgGroupService.findById(item.getPkGroup());
			material.setOrgGroup(orgGroup);

			//计量单位
			BdMeasdoc bdMeasdoc=bdMeasdocService.findById(item.getPkMeasdoc());
			material.setBdMeasdoc(bdMeasdoc);
			//
			materialLst.add(material);



		}
		return materialLst;

	}

	public List<AggMaterial> queryAllByCode(String pk_marbasclass, Integer pageNo, Integer pageSize) {
		List<AggMaterial> materialLst = new ArrayList<>();

		List<BdMaterial> bdMaterialsLst = null;
		try {
			bdMaterialsLst = bdMaterialService.queayAllByClass(pk_marbasclass, pageNo, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			logger.info("传过来的参数不匹配");
		}
		for (BdMaterial item : bdMaterialsLst) {
			AggMaterial material = new AggMaterial();
			material.setBdMaterial(item);

			//物料的组织
			OrgOrgs orgOrgs = orgOrgsService.findById(item.getPkOrg());
			material.setOrgOrgs(orgOrgs);
			//物料的集团
			OrgGroup orgGroup = orgGroupService.findById(item.getPkGroup());
			material.setOrgGroup(orgGroup);
			//计量单位
			BdMeasdoc bdMeasdoc=bdMeasdocService.findById(item.getPkMeasdoc());
			material.setBdMeasdoc(bdMeasdoc);

			materialLst.add(material);

		}
		return materialLst;
	}
}


